import cons from '../constan';
import { routerRedux } from 'dva/router';
import {request} from '../utils/request';
export default {
    namespace: 'pagedata',
    state: {
        nowpage: cons.HomePage,
        rolpic: [],
        themeinfo:[],
        classinfo:[]
    },

    reducers: {
      save(state, { payload: { num } }) {
        let kk = state.kk + num;
        return { ...state, kk };
      },
      changep( state, { payload: wselect }){
          let nowpage = wselect;
          return { ...state, nowpage}
      },
      geti(state,{payload:dataset}) {
          return {...state, ...dataset};
      }
    },

    effects: {
        *openClassDetail({payload:id}, {put}){
          yield put(routerRedux.push(`/classinfo/${id}`));
        },
        *changepage ( {payload:wselect}, {put}){
            // console.log('chagepage');
            let paths;
            if (wselect === cons.HomePage) paths = '/';
            if (wselect === cons.ClassPage) {
              yield put({type: 'classrelation/getrelation'});
              paths = '/class';
            }
            if (wselect === cons.MyPage) paths ='/my';
            yield put(routerRedux.push(paths));
            yield put({type: 'changep',payload:wselect });
        },
        *getinfo ({payload},{put}){
          // get rolpicdata
          let {data:{rolpic,themeinfo,classinfo}} = yield request("http://127.0.0.1:7001/api/v1/home",{
            method: 'GET'
          });
          // for rollingpic
          let tmppic = [];
          rolpic.map( index => {
            let {url} = index;
            tmppic.push(url);
            return index;
          })
          rolpic = tmppic;

          //put data
          yield put({type: 'geti',payload: {rolpic,themeinfo,classinfo}});

        }
    },
    subscriptions: {
      setup({ dispatch, history }) {  // eslint-disable-line
        dispatch({type: 'getinfo',payload:'k'})
      },
    },
    /**
    effects: {
      *fetch({ payload }, { call, put }) {  // eslint-disable-line
        yield put({ type: 'save' });
      },
    },
    */
  };
